//
//  HomeBottomBannerView.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2018/12/20.
//  Copyright © 2018 Gox_WallyDiao. All rights reserved.
//

import UIKit
import SnapKit

class HomeBottomBannerView: UIView, UIScrollViewDelegate {
    //  MARK: - ###### 产品寿命 ######
    
    init(frame: CGRect, message: [String]?) {
        super.init(frame: frame)
        messageArr = message
        renderSubviews()
        bgScrollView.frame = frame
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  MARK: - ###### 子视图 ######
    private func renderSubviews(){
        renderBGScrollView()
        renderIndication()
        addTimer()
    }
    
    fileprivate lazy var bgScrollView: UIScrollView = {
        let tmpScrollView: UIScrollView = UIScrollView(frame: self.bounds)
        tmpScrollView.isPagingEnabled = true
        tmpScrollView.delegate = self
        tmpScrollView.showsVerticalScrollIndicator = false
        tmpScrollView.showsHorizontalScrollIndicator = false
        tmpScrollView.bounces = false
        
        return tmpScrollView
    }()
    
    //  MARK: - ###### 变量 ######
    fileprivate var messageArr: [String]?
    fileprivate var timer: Timer!
    typealias clickAction = (_ index: NSInteger, _ type: String, _ detailUrl: String) -> ()
    var clickAction: clickAction?
    
}

//  MARK: - ###### 协议 ######
extension HomeBottomBannerView {
    //  MARK: *****  UIScrollViewDelegate
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let index:Int = Int(bgScrollView.contentOffset.y / WELFAREBANNER_HEIGHT)
        let pictureCount = (messageArr?.count)!
        if index == 0 {
            bgScrollView.contentOffset = CGPoint(x: 0, y: Int(Float(WELFAREBANNER_HEIGHT) * Float(pictureCount)))
        } else if index == pictureCount + 1 {
            bgScrollView.contentOffset = CGPoint(x: 0, y: WELFAREBANNER_HEIGHT)
        } else {
        }
        addTimer()
    }
    
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        
    }
    
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        delTimer()
    }
    
}

//  MARK: - ###### 事件处理 ######
extension HomeBottomBannerView {
    
}

//  MARK: - ###### 私有方法 ######
extension HomeBottomBannerView {
    fileprivate func renderBGScrollView() {
        self.addSubview(bgScrollView)
        let pictureCount = messageArr?.count
        
        var y: CGFloat = self.bounds.size.height
        if messageArr != nil && pictureCount! > 0  {
            bgScrollView.contentSize = CGSize(width: 0, height: WELFAREBANNER_HEIGHT * CGFloat(pictureCount! + 2))
            if pictureCount! == 1 {
                bgScrollView.isScrollEnabled = false
            }else{
                bgScrollView.isScrollEnabled = true
            }
            var last: UIButton?
            var first: UIButton?
            let height = self.bounds.size.height
            
            for index in 0..<pictureCount! {
                let button = UIButton(type: UIButton.ButtonType.custom)
                button.frame = CGRect(x: 0, y: y, width: self.bounds.size.width, height: height)
                button.tag = 200 + index
                let url = messageArr?[index] ?? ""
                let text:NSString?
                text = "通知：\(url)" as NSString
                
                if text == nil {
                    button.setTitle(text! as String, for: .normal)
                    button.setTitleColor(UIColor.black, for: .normal)
                    button.titleLabel?.font = UIFont.init(name: "PingFangSC-Light", size: 14)
                    button.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment(rawValue: 1)!
                    
                }else{
                    button.setTitle(text! as String, for: .normal)
                    button.setTitleColor(UIColor.black, for: .normal)
                    button.titleLabel?.font = UIFont.init(name: "PingFangSC-Light", size: 14)
                    button.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment(rawValue: 1)!
                    
                }
                
                
                bgScrollView.addSubview(button)
                
                y += height
                if first == nil {
                    first = button
                }
                last = button
            }
            
            if last != nil {
                let url = messageArr?.first ?? ""
                
                let button = UIButton(type: UIButton.ButtonType.custom)
                
                
                let text:NSString?
                
                text = "通知：\(url)" as NSString
                if text == nil {
                    button.setTitle(text! as String, for: .normal)
                    button.setTitleColor(UIColor.black, for: .normal)
                    button.titleLabel?.font = UIFont.init(name: "PingFangSC-Light", size: 14)
                    button.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment(rawValue: 1)!
                }else{
                    button.setTitle(text! as String, for: .normal)
                    button.setTitleColor(UIColor.black, for: .normal)
                    button.titleLabel?.font = UIFont.init(name: "PingFangSC-Light", size: 14)
                    button.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment(rawValue: 1)!
                    
                }
                
                bgScrollView.addSubview(button)
                
                button.frame = CGRect(x: 0, y: y, width: self.bounds.size.width, height: height)
                
                
            }
            
            if first != nil {
                let url = messageArr?.last ?? ""
                
                let button = UIButton(type: UIButton.ButtonType.custom)
                
                let text:NSString?
                text = "通知：\(url)" as NSString
                if text == nil {
                    button.setTitle(text! as String, for: .normal)
                    button.setTitleColor(UIColor.black, for: .normal)
                    button.titleLabel?.font = UIFont.init(name: "PingFangSC-Light", size: 14)
                    button.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment(rawValue: 1)!
                }else{
                    button.setTitle(text! as String, for: .normal)
                    button.setTitleColor(UIColor.black, for: .normal)
                    button.titleLabel?.font = UIFont.init(name: "PingFangSC-Light", size: 14)
                    button.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment(rawValue: 1)!
                    
                }
                bgScrollView.addSubview(button)
                
                button.frame = CGRect(x: 0, y: 0, width: self.bounds.size.width, height: height)
            }
        } else {
            let button = UIButton(type: UIButton.ButtonType.custom)
            //            button.setBackgroundImage(UIImage.init(named: "Home_banner_default"), for: UIControlState.normal)
            
            
            button.tag = 200 + 0
            bgScrollView.addSubview(button)
            
            button.frame = CGRect(x: 0, y: 0, width: self.bounds.size.width, height: self.bounds.size.height)
        }
    }
    
    fileprivate func renderIndication() {
        
        
        if messageArr == nil {
            return ;
        }
        


        bgScrollView.contentOffset = CGPoint(x: 0, y: WELFAREBANNER_HEIGHT)
        
        
    }
    func addTimer() {
        if messageArr == nil || messageArr?.count == 1 {
            return ;
        }
        
        timer = Timer.init(timeInterval: 4, target: self, selector: #selector(self.updatebanner), userInfo:nil, repeats: true)
        RunLoop.current.add(self.timer!, forMode: RunLoop.Mode.common)
    }
    
    @objc func updatebanner()  {
        weak var weakSelf = self
        
        let y = weakSelf!.bgScrollView.contentOffset.y + WELFAREBANNER_HEIGHT
        let index:Int = Int(weakSelf!.bgScrollView.contentOffset.y / WELFAREBANNER_HEIGHT)
        if index >= (self.messageArr?.count)! {
            weakSelf?.bgScrollView.contentOffset = CGPoint(x: 0, y: 0)
        }else{
            UIView.animate(withDuration: 3, delay: 0, options: UIView.AnimationOptions.curveEaseInOut, animations: {
                weakSelf?.bgScrollView.contentOffset = CGPoint(x: 0, y: y)
            }, completion: nil)
        }
        
    }
    
    func delTimer() {
        if timer != nil {
            timer.invalidate()
            timer = nil
        }
    }
}

//  MARK: - ###### 公有方法 ######
extension HomeBottomBannerView {
    
}
